<template>
  <view>
    <!-- 商品列表渲染 -->
    <view class="goods-list">
      <view class="cart-item" v-for="item in cart" :key="item.goods_id">
        <!-- radio 小程序组件库自带的 -->
        <radio
          :checked="item.goods_checked"
          @click="cn_radio(item)"
          color="#EB4450"
        />
        <view class="cart-count">
          <u-number-box
            :min="1"
            :max="105"
            @change="cn_number(item, $event)"
            @overlimit="cn_overlimit(item, $event)"
            :value="item.goods_number"
          />
        </view>
        <!-- hover-class="none" 取消链接组件按下时的灰色样式 -->
        <navigator
          class="goods-item"
          hover-class="none"
          :url="`/pages/index/index`"
        >
          <!-- 商品图片 -->
          <image
            class="goods-image"
            :src="item.goods_small_logo"
            mode="aspectFit"
          />
          <!-- 商品信息 -->
          <view class="goods-info">
            <view class="goods-name">{{ item.goods_name }}</view>
            <view class="goods-price">{{ item.goods_price }}</view>
          </view>
        </navigator>
      </view>
    </view>
    <!-- 底部操作栏占位 -->
    <view class="bottom-bar">
      <!-- 固定定位的操作栏  -->
      <view class="bottom-bar-fixed">
        <view class="select">
          <radio
            class="select-radio"
            @click="cn_AllChecked = !cn_AllChecked"
            :checked="cn_AllChecked"
            color="#EB4450"
          >
            全选
          </radio>
          <text class="select-radio"></text>
        </view>
        <!-- 合计 -->
        <view class="total">
          <text class="total-name">合计：</text>
          <text class="total-price">{{ cn_CheckedGoodsPirce }}</text>
        </view>
        <!-- 结算 -->
        <view class="account">去结算{{ cn_CheckedGoodsCount }}</view>
      </view>
    </view>

    <view>
      <!-- <u-modal
        :show="show"
        :showCancelButton="true"
        :title="title"
        cancelText="取消"
        @confirm="confirm"
      >
        <view class="slot-content">
          <rich-text :nodes="content"></rich-text>
        </view>
      </u-modal> -->
    </view>
  </view>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  //
  data() {
    return {
      cart: []
    }
  },
  onShow() {
    this.cart = JSON.parse(JSON.stringify(this.$store.state.cart))
    console.log(this.cart, '666');
  },
  computed: {
    cn_AllChecked: {
      get() {
        return this.cart.every(item => item.goods_checked)
      },
      set(value) {
        this.cart.forEach(item => item.goods_checked = value)
      }
    },
    ...mapGetters(['cn_CheckedGoodsCount', 'cn_CheckedGoodsPirce'])
  },
  methods: {
    cn_radio(item) {
      item.goods_checked = !item.goods_checked
      this.$store.commit('cn_changeCart', this.cart);
    },

    cn_number(item, e) {
      item.goods_number = e.value
      this.$store.commit('cn_changeCart', this.cart);
    },
    cn_overlimit(item, e) {
      if (e === 'minus') {
        wx.showModal({
          title: '警告',
          content: '确定要删除改商品吗?',
          success: (res) => {
            if (res.confirm) {
              this.cart.splice(this.cart.indexOf(item), 1)
              this.$store.commit('cn_changeCart', this.cart);
            } else if (res.cancel) {
            }
          }
        })
      } else {
        this.$u.toast('不能在添加了!!!');
      }
    },


  }



};
</script>

<style lang="scss">
// 商品列表
.goods-list {
  .goods-item {
    display: flex;
    padding: 25rpx;
    .goods-image {
      width: 190rpx;
      height: 190rpx;
    }
    .goods-info {
      flex: 1;
      font-size: 24rpx;
      // 垂直方向两端对齐
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 20rpx;
      .goods-name {
        // 多行超出隐藏
        overflow: hidden;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .goods-price {
        color: #eb4450;
        // 前面的价格符号
        &::before {
          content: '￥';
          font-size: 80%;
          margin-right: 5rpx;
        }
      }
    }
  }
}
// 购物车列表项
.cart-item {
  display: flex;
  align-items: center;
  padding-left: 20rpx;
  border-bottom: 1rpx solid #ddd;
  position: relative;
  // 清除最后一项的底边
  &:last-child {
    border-bottom: none;
  }
  // 步进器外层盒子，定位实现
  .cart-count {
    position: absolute;
    // 体验优化细节：扩大盒子范围，防止用户误操作
    // background-color: pink;
    padding: 20rpx;
    bottom: 0;
    right: 0;
  }
}

// 底部操作栏
.bottom-bar {
  // 占位用
  height: 100rpx;
  // 定位的操作栏
  .bottom-bar-fixed {
    height: 80rpx;
    position: fixed;
    z-index: 99;
    left: 0;
    right: 0;
    bottom: 0;

    border-top: 1px solid #eee;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 20rpx;

    .select {
      .select-radio {
        font-size: 24rpx;
        color: #999;
      }
    }

    .total {
      flex: 1;
      display: flex;
      align-items: center;
      margin-left: 40rpx;
      .total-name {
        font-size: 24rpx;
      }

      .total-price {
        color: #eb4450;
      }
    }

    .account {
      background-color: #eb4450;
      color: #fff;
      font-size: 22rpx;
      width: 150rpx;
      height: 52rpx;
      border-radius: 26rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
